package com.ziq.zcamp.dialog

import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.qweather.sdk.bean.weather.WeatherDailyBean
import com.qweather.sdk.view.QWeather
import com.ziq.base.base.BaseDialog
import com.ziq.zcamp.databinding.DialogWeatherBinding
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.zcamp.manager.PrivacyManager
import com.ziq.base.extension.*
import com.ziq.zcamp.R
import com.ziq.zcamp.databinding.DialogWeatherItemBinding
import com.ziq.zcamp.util.TimeUtil.Companion.MM_dd_FORMATTER
import com.ziq.zcamp.util.TimeUtil.Companion.getWeekDay
import com.ziq.zcamp.util.TimeUtil.Companion.isToday
import com.ziq.zcamp.util.TimeUtil.Companion.yyyy_MM_dd_DATE_FORMATTER
import com.ziq.zcamp.util.WeatherUtil

class WeatherDialog(context: Context, var longitude :String, var latitude :String) : BaseDialog(context) {

    private val binding: DialogWeatherBinding by binding()
    var adapter = WeatherItemAdapter()
    val dateList: MutableList<WeatherDailyBean.DailyBean> = mutableListOf()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.root
        setAnimateView(binding.root)
        binding.recyclerView.layoutManager = GridLayoutManager(context, 7)
        binding.recyclerView.adapter = adapter
        if(PrivacyManager.isAgreedPrivacy()){
            QWeather.getWeather7D(GlobalContextManager.appContext, "$longitude,$latitude", object : QWeather.OnResultWeatherDailyListener {
                override fun onSuccess(weatherDailyBean: WeatherDailyBean) {
                    dateList.clear()
                    dateList.addAll(weatherDailyBean.daily)
                    adapter.setData(weatherDailyBean.daily)
                    binding.temperatureLineView.setWeatherData(dateList)
                }

                override fun onError(throwable: Throwable?) {
                }
            }
            )
        }

    }

    class WeatherItemAdapter() : RecyclerView.Adapter<BindingViewHolder<DialogWeatherItemBinding>>(){

        val dataList: MutableList<WeatherDailyBean.DailyBean> = mutableListOf()

        fun setData(list: List<WeatherDailyBean.DailyBean>){
            dataList.clear()
            dataList.addAll(list)
            notifyDataSetChanged()
        }

        override fun getItemCount(): Int {
            return dataList.size
        }

        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): BindingViewHolder<DialogWeatherItemBinding> {
            return createBindingViewHolder(parent)
        }

        override fun onBindViewHolder(
            holder: BindingViewHolder<DialogWeatherItemBinding>,
            position: Int
        ) {
            val item = dataList[position]
            try {
                val date = yyyy_MM_dd_DATE_FORMATTER.parse(item.fxDate)
                if(isToday(date!!)){
                    holder.binding.tvWeek.text = "今天"
                }else {
                    holder.binding.tvWeek.text = getWeekDay(date)
                    holder.binding.container.setBackgroundResource(R.drawable.bg_weather_item)
                }
                holder.binding.tvDate.text = MM_dd_FORMATTER.format(date)
            }catch (e:Exception){}

            holder.binding.tvWeather.text = item.textDay
            holder.binding.ivWeather.setImageResource(WeatherUtil.getWeatherResByCode(item.iconDay))
        }
    }

    override fun initWidth(): Int {
        return ViewGroup.LayoutParams.MATCH_PARENT
    }

    override fun initGravity(): Int {
        return Gravity.BOTTOM or Gravity.CENTER
    }

}